package com.xiaotao.utils;

import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class ClassUtils {
    public static List<String> scanClass(String path) {
        String origin = "/" + path.replace(".", "/");
        List<String> res = new LinkedList<>();
        LinkedList<String> dirs = new LinkedList<>();
        String cur = "";
        String basePath = ClassUtils.class.getResource("/").getPath() + "/" + origin;
        StringBuilder fullPath = new StringBuilder();
        StringBuilder fullClassName = new StringBuilder();
        dirs.addLast(cur);
        do {
            cur = dirs.getFirst();
            fullPath.delete(0, fullPath.length());
            fullPath.append(basePath).append("/").append(cur);

            File dir = new File(fullPath.toString());
            for(File file:dir.listFiles()){
                if (file.isDirectory()) {
                    dirs.addLast(cur + "/" + file.getName());
                } else if (file.getName().endsWith(".class")){
                    fullClassName.delete(0, fullClassName.length());
                    fullClassName.append(path).append(cur.replace("/",".")).append(".").append(file.getName().replace(".class",""));
                    res.add(fullClassName.toString());

                }
            }
            dirs.removeFirst();
        } while ( !dirs.isEmpty() );
        return res;
    }
}
